﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModulA.ViewModels
{
    internal class ViewCViewModel : BindableBase, IDialogAware
    {
        //标题
        public string Title { get; set; } = "这是一个标题";

        //关闭时的委托
        public event Action<IDialogResult> RequestClose;

        //命令
        public DelegateCommand ConfirmCmd { get; set; }
        public DelegateCommand CancelCmd { get; set; }


        //属性
        private string para;

        public string Para
        {
            get { return para; }
            set
            {
                para = value;
                RaisePropertyChanged();
            }
        }


        //构造函数
        public ViewCViewModel()
        {
            ConfirmCmd = new DelegateCommand(ConfirmFunc);
            CancelCmd = new DelegateCommand(CancelFunc);
        }


        /// <summary>
        /// 是否能够取消对话框
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool CanCloseDialog()
        {
            return true;
        }

        //取消时的具体方法
        private void CancelFunc()
        {
            DialogParameters paras = new DialogParameters();
            paras.Add("CancelPara", "取消的参数");
            RequestClose?.Invoke(new DialogResult(ButtonResult.No, paras));
        }

        //关闭时的具体方法
        private void ConfirmFunc()
        {
            DialogParameters paras = new DialogParameters();
            paras.Add("ConfirmPara", "确定的参数");
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, paras));
        }

        /// <summary>
        /// 关闭后执行--反向传参
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public void OnDialogClosed()
        {
            //触发直接关闭的按钮
            DialogParameters paras = new DialogParameters();
            paras.Add("ClosePara", "直接关闭的参数");

            //当直接关闭的时候，窗口已经消失了，RequestClose为null，不会执行invoke方法，因此不会传参。
            RequestClose?.Invoke(new DialogResult(ButtonResult.None, paras));
        }

        /// <summary>
        /// 打开后执行--正向传参
        /// </summary>
        /// <param name="parameters"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
            Para = parameters.GetValue<string>("Para1");
        }
    }
}
